<?php

namespace App\Console;

use DB;

class AdminPermissionDataInitCommand extends AdminDataInitCommand
{
    protected $relatedTables = [];

    public function handle()
    {
        DB::transaction(function () {
            DB::statement('SET FOREIGN_KEY_CHECKS = 0');
            $this->model::truncate();

            $this->model::insert($this->data());

            $this->handleRelation();

            DB::statement('SET FOREIGN_KEY_CHECKS = 1');
        });
    }

    protected function handleRelation()
    {
        if ($this->option('force')) {
            foreach ($this->relatedTables as $table) {
                DB::table($table)->truncate();
            }
        }
    }
}
